var city;
var type;
function drawTable(arrayInfo){ //Ham ve table
    //var myHTML="";
    for(var i=0; i<arrayInfo.length; i++){
        var myHTML = "<div class='testimonial-item Male' id='"+i+"'><div class='testimonial-content' id='c"+i+"'>"
        +"<p class='col-220-title'><a>Driver Listing</a></p><table>"
        + "<tr><td style='width: 90px;text-align: right;'>Username :</td>"
        + "<td style='padding-left:20px;'>"+arrayInfo[i].userJName+"</td></tr>"
        + "<tr><td style='text-align: right;'>Address :</td>"
        + "<td style='padding-left:20px;'>"+arrayInfo[i].userJAddress+"</td></tr>"
        + "<tr><td style='text-align: right;'>Mobile :</td>"
        + "<td style='padding-left:20px;'>"+arrayInfo[i].userJMobile+"</td></tr>"
        + "<tr><td style='text-align: right;'>Email :</td>"
        + "<td style='padding-left:20px;'>"+arrayInfo[i].userJEmail+"</td></tr>"
        + "</table></div></div>";
        jQuery(".white-swap1").append(myHTML);
        window.scrollTo(100, 200);
    }
}

function createXmlHttpRequestObject() {
    var ro;
    var browser = navigator.appName;
    // Need to determine IE7 and not do this.
    if (browser == "Microsoft Internet Explorer") {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else {
        ro = new XMLHttpRequest();
    }
    return ro;
}

function createResponseXML(textXML) {
    // code for IE
    if (window.ActiveXObject) {
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(textXML);
        return xmlDoc;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else {
        var parser = new DOMParser();
        var xmlDoc = parser.parseFromString(textXML, "text/xml");
        return xmlDoc;
    }
}
function searchListing(city,type){
    var url = '/RadioCabsProject-war/SearchEngineServlet?city='+ city + '&type=' + type;
    //alert(url);
    aeRequest = createXmlHttpRequestObject();
    aeRequest.open("POST", url, true);
    aeRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    aeRequest.onreadystatechange = finishUpdate;
    aeRequest.send(null);
}

function finishUpdate(){
    if(aeRequest.readyState==4){
        if(aeRequest.status == 200){
            //var response = createResponseXML(aeRequest.responseText);
            var JSONString = JSON.parse(aeRequest.responseText);
            if(JSONString != null && JSONString.length>0){
                drawTable(JSONString);
            }
        }
    }
}

function loginUser(type,username,password){
    actionType = type;
    var url = '/RadioCabsProject-war/LoginServlet?type='+ type +'&username=' +username + '&password=' + password;
    //alert(url);
    aeRequest = createXmlHttpRequestObject();
    aeRequest.open("POST", url, true);
    aeRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    aeRequest.onreadystatechange = resultUser;
    aeRequest.send(null);
}

function logoutUser(){
    var url = '/RadioCabsProject-war/LogoutServlet';
    //alert(url);
    aeRequest = createXmlHttpRequestObject();
    aeRequest.open("POST", url, true);
    aeRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    aeRequest.onreadystatechange = resultUser;
    aeRequest.send(null);
}

function resultUser(){
    if(aeRequest.readyState==4){
        if(aeRequest.status == 200){
            var response = createResponseXML(aeRequest.responseText);
            var result = response.getElementsByTagName("result")[0].firstChild.nodeValue;
            if (result == "success") {
                window.location.href= "driversearch.jsp";
            }else{
                alert("Login failed!");
            }
        }
    }
}
jQuery(document).ready(function(){
    jQuery("#btnSearchDriver").click(function(){
        jQuery(".white-swap1").html("");
        city = jQuery("#searchDriver").val();
        type = "Driver";
        searchListing(city,type);
    });

    jQuery("#btnLogin").live('click',function(){
        usernameRC = jQuery("#txtUser").val();
        passwordRC = jQuery("#txtPass").val();
        typeRC = jQuery("#listUserType").val();

        loginUser(typeRC, usernameRC, passwordRC);
    });

    jQuery("#btnLogout").live('click',function(){
        logoutUser();
    });

    jQuery("#listUserType").change(function(){
       if(jQuery(this).val() == "Advertise"){
           //jQuery("#txtPass").fadeOut("slow");
           jQuery("#lbPass").fadeOut("slow");
       }else{
           //jQuery("#txtPass").fadeIn("fast");
           jQuery("#lbPass").fadeIn("slow");
       }
    });

    jQuery("#txtUser").keypress(function(event){
        if(event.which == 13){
            usernameRC = jQuery("#txtUser").val();
            passwordRC = jQuery("#txtPass").val();
            typeRC = jQuery("#listUserType").val();

            loginUser(typeRC, usernameRC, passwordRC);
        }
    });

    jQuery("#txtPass").keypress(function(event){
        if(event.which == 13){
            usernameRC = jQuery("#txtUser").val();
            passwordRC = jQuery("#txtPass").val();
            typeRC = jQuery("#listUserType").val();

            loginUser(typeRC, usernameRC, passwordRC);
        }
    });
})